import { Switch } from 'element-ui';
const switchPatch = {
  name: 'ElSwitchOther',
  extends: Switch, // 继承switch组件
  props: {
    // 传入的beforechange方法
    handleBeforeChange: Function
  },
  methods: {
    async handleChange() {
      // 判断是否存在handleBeforeChange方法
      // 使用Promise来处理
      if (this.handleBeforeChange) {
        const before = await this.handleBeforeChange(this.checked);
        if (before === false) {
          return;
        }
      }
      // 这是源码部分，直接复制过来即可
      const val = this.checked ? this.inactiveValue : this.activeValue;
      this.$emit('input', val);
      this.$emit('change', val);
      this.$nextTick(() => {
        // set input's checked property
        // in case parent refuses to change component's value
        if (this.$refs.input) {
          this.$refs.input.checked = this.checked;
        }
      });
    }
  }
};

export default switchPatch;
